﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using tactics.Interfaces;
using TomShane.Neoforce.Controls;


namespace tactics.GUI
{
    using EffectList = List<IStatusEffect>;
    using CardList = List<StatusCard>;
    using ScreenCoords = Utility.Pair<int, int>;

    class StatusDisplayManager
    {
        public enum LayoutOptions
        {
            Down1C,
            Down2C,
            Up1C,
            Up2C,
        }

        Manager guiManager;
        string xmlFile;

        LayoutOptions layout;

        Unit unit;
        CardList cardList;

        ScreenCoords startCoords;

        #region Properties

        public ScreenCoords RootCoords
        {
            get { return startCoords; }
        }

        #endregion

        public StatusDisplayManager(Manager guiManagerIn, string xmlFileIn, Unit unitIn, LayoutOptions layoutIn, int x, int y)
        {
            guiManager = guiManagerIn;
            xmlFile =  xmlFileIn;

            unit = unitIn;

            layout = layoutIn;

            InitCards();

            startCoords = new ScreenCoords(x, y);
            if (cardList.Count > 0 && (layout == LayoutOptions.Up1C || layout == LayoutOptions.Up2C))
                startCoords.Second -= cardList[0].Height;
        }

        public void Remove()
        {
            HideCards();

            foreach (var card in cardList)
            {
                card.Remove();
            }
        }

        private void InitCards()
        {
            cardList = new CardList();

            for (int i = 0; i < unit.StatusEffects.Count; ++i)
            {
                IStatusEffect current = unit.StatusEffects[i];
                cardList.Add(new StatusCard(guiManager, xmlFile, current));
            }
        }

        #region Display

        public void Adjust(int x, int y)
        {
            startCoords.First += x;
            startCoords.Second += y;

            foreach (StatusCard card in cardList)
            {
                card.UpdatePosition(card.Position.First + x, card.Position.Second + y);
            }
        }

        public void ShowCards()
        {
            // turning on cards for a unit
            if (unit != null && unit.StatusEffects.Count != 0)
            {
                int y = 0;
                int x = 0;
                int count = 1;

                // foreach ability in unit
                foreach (StatusCard card in cardList)
                {
                    card.Visible = true;
                    card.UpdatePosition(startCoords.First + (x * (card.Width + 2)), startCoords.Second + (y * (card.Height + 2)));

                    // kind of ugly. gets the next x/y depending on which layout we're using.
                    if (layout == LayoutOptions.Down2C || layout == LayoutOptions.Up2C)
                    {
                        if (count % 2 != 0)
                            ++x;
                        else
                        {
                            x = 0;
                            if (layout == LayoutOptions.Down2C)
                                ++y;
                            else
                                --y;
                        }
                    }
                    else
                    {
                        if (layout == LayoutOptions.Down1C || layout == LayoutOptions.Down2C)
                            ++y;
                        else
                            --y;
                    }

                    ++count;
                }
            }
            else
                HideCards();
        }

        public void HideCards()
        {
            foreach (StatusCard card in cardList)
            {
                card.Visible = false;
            }
            cardList.Clear();
            unit = null;
        }

        #endregion
    }
}
